<?php

// 完善项目信息
// PUT project.complete

namespace app\api\resource\project;
use think\Db;
use think\Loader;
use think\Session;

class Complete
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['login'];
    // 描述
    public $_description = '完善项目信息';
    // 参数
    public $_param  = [
        'project_id' => '项目主键',
        'company'    => '公司全称',
        'team'       => '团队规模',
        'members'    => '核心团队成员,JsonArray',
        'bp'         => '商业计划书主键',
    ];
    // 返回
    public $_return = [
        201 => '操作成功',
        400 => '项目不存在',
        422 => '请求参数不合法',
        500 => '服务器错误',
    ];

    public function run(&$request)
    {
        $projectId = $request->put('project_id/d',0,'abs');
        if (0 == $projectId) return [422,'项目主键必须'];

        $ret = Db::name('project')->where('id',$projectId)->find();
        if ( is_null($ret) ) return [400,'项目不存在'];

        $params = $request->put();

        $validate = Loader::validate('Project');
        if ( !$validate->scene('complete')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $params['update_time'] = time();
        $num = Db::name('project')->where('id',$projectId)->update($params);
        return ($num > 0) ? [ 201,[] ] : 500;
    }
}